home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / tcsh / Source / sh.print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  4.9 KB  |  221 lines

  1. /* $Header: /u/christos/src/tcsh-6.03/RCS/sh.print.c,v 3.4 1992/10/14 20:19:19 christos Exp $ */
  2. /*
  3.  * sh.print.c: Primitive Output routines.
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: sh.print.c,v 3.4 1992/10/14 20:19:19 christos Exp $")
  40.  
  41. #include "ed.h"
  42.  
  43. extern int Tty_eight_bit;
  44. extern int Tty_raw_mode;
  45. extern Char GettingInput;
  46.  
  47. int     lbuffed = 1;        /* true if line buffered */
  48.  
  49. static    void    p2dig    __P((int));
  50.  
  51. /*
  52.  * C Shell
  53.  */
  54.  
  55. #ifdef BSDLIMIT
  56. void
  57. psecs(l)
  58.     long    l;
  59. {
  60.     register int i;
  61.  
  62.     i = l / 3600;
  63.     if (i) {
  64.     xprintf("%d:", i);
  65.     i = l % 3600;
  66.     p2dig(i / 60);
  67.     goto minsec;
  68.     }
  69.     i = l;
  70.     xprintf("%d", i / 60);
  71. minsec:
  72.     i %= 60;
  73.     xprintf(":");
  74.     p2dig(i);
  75. }
  76.  
  77. #endif
  78.  
  79. void
  80. pcsecs(l)            /* PWP: print mm:ss.dd, l is in sec*100 */
  81. #ifdef BSDTIMES
  82.     long    l;
  83. #else /* BSDTIMES */
  84. # ifndef POSIX
  85.     time_t  l;
  86. # else /* POSIX */
  87.     clock_t l;
  88. # endif /* POSIX */
  89. #endif /* BSDTIMES */
  90. {
  91.     register int i;
  92.  
  93.     i = l / 360000;
  94.     if (i) {
  95.     xprintf("%d:", i);
  96.     i = (l % 360000) / 100;
  97.     p2dig(i / 60);
  98.     goto minsec;
  99.     }
  100.     i = l / 100;
  101.     xprintf("%d", i / 60);
  102. minsec:
  103.     i %= 60;
  104.     xprintf(":");
  105.     p2dig(i);
  106.     xprintf(".");
  107.     p2dig((int) (l % 100));
  108. }
  109.  
  110. static void 
  111. p2dig(i)
  112.     register int i;
  113. {
  114.  
  115.     xprintf("%d%d", i / 10, i % 10);
  116. }
  117.  
  118. char    linbuf[2048];        /* was 128 */
  119. char   *linp = linbuf;
  120. bool    output_raw = 0;        /* PWP */
  121.  
  122. void
  123. xputchar(c)
  124.     register int c;
  125. {
  126.     int     atr = 0;
  127.  
  128.     atr |= c & ATTRIBUTES & TRIM;
  129.     c &= CHAR | QUOTE;
  130.     if (!output_raw && (c & QUOTE) == 0) {
  131.     if (Iscntrl(c)) {
  132.         if (c != '\t' && c != '\n' && c != '\r') {
  133.         xputchar('^' | atr);
  134.         if (c == ASCII)
  135.             c = '?';
  136.         else
  137.             c |= 0100;
  138.         }
  139.     }
  140.     else if (!Isprint(c)) {
  141.         xputchar('\\' | atr);
  142.         xputchar((((c >> 6) & 7) + '0') | atr);
  143.         xputchar((((c >> 3) & 7) + '0') | atr);
  144.         c = (c & 7) + '0';
  145.     }
  146.     (void) putraw(c | atr);
  147.     }
  148.     else {
  149.     c &= TRIM;
  150.     if (haderr ? (didfds ? is2atty : isdiagatty) :
  151.         (didfds ? is1atty : isoutatty))
  152.         SetAttributes(c | atr);
  153.     (void) putpure(c);
  154.     }
  155.     if (lbuffed && (c & CHAR) == '\n')
  156.     flush();
  157. }
  158.  
  159. int
  160. putraw(c)
  161.     register int c;
  162. {
  163.     if (haderr ? (didfds ? is2atty : isdiagatty) :
  164.     (didfds ? is1atty : isoutatty)) {
  165.     if (Tty_eight_bit == -1)
  166.         ed_set_tty_eight_bit();
  167.     if (!Tty_eight_bit && (c & META)) {
  168.         c = (c & ~META) | STANDOUT;
  169.     }
  170.     SetAttributes(c);
  171.     }
  172.     return putpure(c);
  173. }
  174.  
  175. int
  176. putpure(c)
  177.     register int c;
  178. {
  179.     c &= CHAR;
  180.  
  181.     *linp++ = c;
  182.     if (linp >= &linbuf[sizeof linbuf - 10])
  183.     flush();
  184.     return (1);
  185. }
  186.  
  187. void
  188. draino()
  189. {
  190.     linp = linbuf;
  191. }
  192.  
  193. void
  194. flush()
  195. {
  196.     register int unit;
  197.  
  198.     /* int lmode; */
  199.  
  200.     if (linp == linbuf)
  201.     return;
  202.     if (GettingInput && !Tty_raw_mode && linp < &linbuf[sizeof linbuf - 10])
  203.     return;
  204.     if (haderr)
  205.     unit = didfds ? 2 : SHDIAG;
  206.     else
  207.     unit = didfds ? 1 : SHOUT;
  208. #ifdef COMMENT
  209. #ifdef TIOCLGET
  210.     if (didfds == 0 && ioctl(unit, TIOCLGET, (ioctl_t) & lmode) == 0 &&
  211.     lmode & LFLUSHO) {
  212.     lmode = LFLUSHO;
  213.     (void) ioctl(unit, TIOCLBIC, (ioclt_t) & lmode);
  214.     (void) write(unit, "\n", 1);
  215.     }
  216. #endif
  217. #endif
  218.     (void) write(unit, linbuf, (size_t) (linp - linbuf));
  219.     linp = linbuf;
  220. }
  221.